package app.client;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import server.layer.Person;
import server.layer.ServiceProtocol;

/**
 * Created by IntelliJ IDEA.
 * User: Awake
 * Date: 23.04.12
 * Time: 22:40
 * To change this template use File | Settings | File Templates.
 */
public class RequestSender implements iObserver{
    static ServiceClient clientConnection;

    public static void requestDescription(int person_id) {
        sendQuery(ServiceProtocol.GET_DESCRIPTION, person_id);
    }

    public static void requestDeletePerson(Long person_id) {
        sendQuery(ServiceProtocol.DELETE_PERSON, person_id);
    }

    public static void requestAddPerson(Long person_id) {
        sendQuery(ServiceProtocol.ADD_PERSON, person_id);
    }

    public static void requestModifyPerson(Person person) {
        sendQuery(ServiceProtocol.UPDATE_PERSON, person);
    }

    public static void sendNewDescription(String description, int person_id) {
        sendQuery(ServiceProtocol.UPDATE_DESCRIPTION, person_id);
    }

    public static void sendNewImage(byte[] imgData, int person_id){
        sendQuery(ServiceProtocol.UPDATE_IMAGE, imgData , person_id);
    }

    public static void sendQuery(ServiceProtocol command, Object arg) {
        JSONObject obj = new JSONObject();
        JSONObject argument = new JSONObject(arg);
        System.out.println(argument);
        try {
            obj.put("command", command.getAbbr());
            obj.put("argument", argument);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        clientConnection.sendMessage(obj.toString());
    }

    public static void sendQuery(ServiceProtocol command, byte[] arg, int id) {

        try {
            JSONObject obj = new JSONObject();
            JSONArray argument = new JSONArray(arg);
            obj.put("command", command.getAbbr());
            obj.put("argument", argument);
            obj.put("id", id);
            clientConnection.sendMessage(obj.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public static void sendQuery(ServiceProtocol command, int arg) {

        try {
            JSONObject obj = new JSONObject();
            obj.put("command", command.getAbbr());
            obj.put("argument", arg);
            clientConnection.sendMessage(obj.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void sendQuery(ServiceProtocol command) {

        try {
            JSONObject obj = new JSONObject();
            obj.put("command", command.getAbbr());
            clientConnection.sendMessage(obj.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public static void connect() {
        clientConnection = new ServiceClient();
        sendQuery(ServiceProtocol.CONNECT);
    }

    public static void closeConnection() {
        if (clientConnection != null) {
            sendQuery(ServiceProtocol.CLOSE_CONNECTION);
            clientConnection.kill();
        }
    }

    @Override
    public void update(ServiceProtocol command) {

    }
}
